/*
	Copyright (C) 2008 by Jean-François Geyelin
	jfgeyelin@gmail.com

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the
	Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/


#include "all.h"
#include "SurfaceHandler.h"
#include "globals.h"
#include "DisplayString.h"
#include "handleEvents.h"
#include "drawButtonInMenu.h"
#include "settings.h"
#include "changePlayerName.h"
#include "wait.h"


void convertScreenToGrayscale(void) {
    Uint8 r,g,b;
    for (int x=0;x<SCREEN_WIDTH;x++) {
        for (int y=0;y<SCREEN_HEIGHT;y++) {
            SurfaceHandler::getPixel(screenSurface, x, y, &r, &g, &b);


            #define HANDLE_IT(c)\
            {\
            int temp = c;\
            temp/=16;\
            temp+=240;\
            if (temp>255)\
            temp = 255;\
            if (temp<0)\
            temp = 0;\
            c = temp;\
            }

            HANDLE_IT(r)
            HANDLE_IT(g)
            HANDLE_IT(b)


            SurfaceHandler::putpixel3(screenSurface, x, y, SDL_MapRGB( screenSurface->format, r,g,b ));
        }
    }
}



bool submitScoreMenu(int points, int time, int replayNumber) {

    KeyPressed keyPressed;
    do {
        SDL_Delay(20);
        keyPressed.readKeys();
        handleEvents();
    } while (keyPressed.action);


    int score = points+time;
    convertScreenToGrayscale();
    SDL_Surface *screenSave;
    screenSave = SDL_CreateRGBSurface(SDL_HWSURFACE, screenSurface->w, screenSurface->h, 32, bmask, gmask, rmask, amask);
    SurfaceHandler::drawSurface(screenSurface, 0, 0, screenSave);


    char str[500];
    Settings settings;




    loadSettings(&settings);

    sprintf(str,"You scored %i (%i + %i). Your time is %i",score, points, time,5000-time);
    DisplayString::drawString3(str, (SCREEN_WIDTH-strlen(str+4)*FONT_SPACING)/2, (SCREEN_HEIGHT*4)/10-30);
    sprintf(str,"Submit score and replay #%i as \"%s\" ?",replayNumber,settings.playerName);
    DisplayString::drawString3(str, (SCREEN_WIDTH-strlen(str)*FONT_SPACING)/2, (SCREEN_HEIGHT*4)/10);

    bool submit = false;
    quit = false;

    int mouseX, mouseY;
    bool mouseIsPressed;

    int y = ((SCREEN_HEIGHT*4)/10)+30;


    int x = 100;
    int dx = (SCREEN_WIDTH - 2*x - 120)/2;
    int value = 0, previousValue = 0;



    while (!quit) {
        keyPressed.readKeys();
        mouseIsPressed = SDL_GetMouseState(&mouseX,&mouseY)&SDL_BUTTON(1);
        previousValue = value;
        value = 0;
        value |= drawButtonInMenu(mouseX, mouseY, x, y, 120 , 20 , "Cancel", screenSurface,1,1);
        value |= drawButtonInMenu(mouseX, mouseY, x + dx, y, 120 , 20 , "Change name", screenSurface,0,2);
        value |= drawButtonInMenu(mouseX, mouseY, x + 2*dx, y, 120 , 20 , "Submit", screenSurface,0,4);



        if (value != 0 && value != previousValue) {
            allSounds->play("menu");
        }

        if (mouseIsPressed || keyPressed.action) {
            if (keyPressed.action)
            value = 4;
            switch (value) {
                case 2:
                    changePlayerName();
                    SurfaceHandler::drawSurface(screenSave, 0, 0, screenSurface);
                    loadSettings(&settings);
                    sprintf(str,"Submit score and replay #%i as \"%s\" ?",replayNumber,settings.playerName);
                    DisplayString::drawString3(str, (SCREEN_WIDTH-strlen(str)*FONT_SPACING)/2, (SCREEN_HEIGHT*4)/10);
                    break;
                case 4:
                    submit = true;
                case 1:
                    quit = true;
                    break;
            }
        }

        SDL_Delay( 15 );
 		SDL_Flip(screenSurface );
 		handleEvents();
    }
    SurfaceHandler::drawSurface(screenSave, 0, 0, screenSurface);
    SDL_FreeSurface(screenSave);
    waitForKeyRelease();
    return submit;

}
